﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils.URLBuilder;

namespace Framework.Portal.Layouts.Admin.Components.BGTT.OnlineExam
{
    public partial class ExamsManager : BaseControl
    {
        private static TeenioPermission curPermision = new TeenioPermission();
        private readonly string MENUNAME = string.Empty;
        private int _curPage;
        protected string arrayArticle = "{}";
        List<PortalUser> lstUser = new List<PortalUser>();
        public ExamsManager()
        {
        }

        public ExamsManager(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtFromDate.Text = DateTime.Now.AddDays(-21).ToString("dd/MM/yyyy hh:mm");
            txtToDate.Text = DateTime.Now.AddDays(1).ToString("dd/MM/yyyy hh:mm");
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            try
            {

                List<BGTTExam> lst = ManagerFactory.DAOEntity.GetList<BGTTExam>(new BGTTExam() { OrderBy = "ID DESC" });
                if (lst != null)
                {
                    //arrayArticle = string.Empty;
                    rptDistributed.DataSource = lst;
                    rptDistributed.DataBind();
                }
                else
                {
                    rptDistributed.DataSource = null;
                    rptDistributed.DataBind();
                }

                //
                //if (lst != null)
                //{
                //    foreach (var info in lst)
                //    {
                //        Response.Write(info.TITLE + "<br />");
                //    }
                //}
            }
            catch (Exception ex) { }
        }

        protected void rptDistributed_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblID = (Label)e.Item.FindControl("lblID");
                var imgAvatar = (Image)e.Item.FindControl("imgAvatar");
                var lblTitle = (Label)e.Item.FindControl("lblTitle");
                var lblStt = (Label)e.Item.FindControl("lblStt");
                var lblStart = (Label)e.Item.FindControl("lblStart");
                var lblEnd = (Label)e.Item.FindControl("lblEnd");
                var lblToTalQ = (Label)e.Item.FindControl("lblToTalQ");
                var nsPublish = (ImageButton)e.Item.FindControl("ImageButton1");
                var nsUnPublish = (ImageButton)e.Item.FindControl("btnImgManagePassword");
                var nsAuto = (ImageButton)e.Item.FindControl("imgBtnAutoExam");

                BGTTExam info = (BGTTExam)e.Item.DataItem;
                lblID.Text = info.ID.ToString();
                lblTitle.Text = info.TITLE;
                if (info.STATUS == 0)
                {
                    lblStt.Text = "Chưa kích hoạt";
                    lblStt.ForeColor = System.Drawing.Color.Red;
                    nsPublish.Visible = true;
                    nsAuto.Visible = true;
                    nsUnPublish.Visible = false;
                }
                else if (info.STATUS == 1)
                {
                    lblStt.Text = "Đang thi";
                    lblStt.ForeColor = System.Drawing.Color.Green;
                    nsUnPublish.Visible = true;
                    nsAuto.Visible = true;
                    nsPublish.Visible = false;
                }
                else
                {
                    lblStt.Text = "Đã thi xong";
                    lblStt.ForeColor = System.Drawing.Color.Orange;
                    nsUnPublish.Visible = true;
                    nsPublish.Visible = true;
                    nsAuto.Visible = false;
                }
                DateTime start = ConvertUtility.ToDateTime(info.STARTDATE);
                DateTime end = ConvertUtility.ToDateTime(info.ENDDATE);
                lblStart.Text = start.Day + "-" + start.Month + "-" + start.Year + "  <span style='font-style: italic;font-size: 10px;'>(" + start.Hour + ":" + start.Minute + ")</span>";
                lblEnd.Text = end.Day + "-" + end.Month + "-" + end.Year + "  <span style='font-style: italic;font-size: 10px;'>(" + end.Hour + ":" + end.Minute + ")</span>";
                lblToTalQ.Text = info.TOTALQUESTION.ToString();
            }
        }

        protected void rptDistributed_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int xID = ConvertUtility.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "edit":
                    var urlParser = new SimpleURLParameterParser(Request.RawUrl);
                    var lstUrl = new Dictionary<string, string>();
                    lstUrl.Add("t", "1");
                    lstUrl.Add("a", xID.ToString());
                    Response.Redirect(urlParser.CreateQueryString(lstUrl));
                    break;
                case "publish":
                    ManagerFactory.DAOEntity.Update(new BGTTExam
                    {
                        ID = xID,
                        STATUS = 1
                    });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    BindData();
                    break;
                case "remove":
                    ManagerFactory.DAOEntity.Delete(new BGTTExam
                    {
                        ID = xID
                    });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    BindData();
                    break;
                case "unpublish":
                    ManagerFactory.DAOEntity.Update(new BGTTExam
                    {
                        ID = xID,
                        STATUS = 0
                    });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    BindData();
                    break;
                case "auto":
                    ManagerFactory.DAOEntity.Update(new BGTTExam
                    {
                        ID = xID,
                        STATUS = 2
                    });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    BindData();
                    break;
                default:
                    break;
            }
        }

    }
}